
package zhongbo.java.air.dictionary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import zhongbo.java.air.dictionary.mapper.AirCraftMapper;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.service.IAirCraftService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 说明：
 * 飞机信息服务实现类
 *
 * @author 钟波dellNotebook
 * @since 2025年03月31日 星期一 08:29:12
 */
@Service
public class AirCraftServiceImpl extends ServiceImpl<AirCraftMapper, AirCraft> implements IAirCraftService {

    @Override
    public Page<AirCraft> pageWithOrder(Page<AirCraft> page, String orderField, boolean isAsc) {
        QueryWrapper<AirCraft> queryWrapper = new QueryWrapper<>();
        
        if (orderField != null && !orderField.isEmpty()) {
            queryWrapper.orderBy(true, isAsc, orderField);
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        
        return page(page, queryWrapper);
    }

    @Override
    public IPage<AirCraft> pageWithOrder(IPage<AirCraft> page, String orderField, boolean isAsc) {
        QueryWrapper<AirCraft> queryWrapper = new QueryWrapper<>();
        
        if (orderField != null && !orderField.isEmpty()) {
            queryWrapper.orderBy(true, isAsc, orderField);
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        
        return page(page, queryWrapper);
    }

    @Override
    public AirCraft getByIcaoCode(String icaoCode) {
        LambdaQueryWrapper<AirCraft> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AirCraft::getIcaoCode, icaoCode);
        return getOne(queryWrapper);
    }

    @Override
    public List<AirCraft> listByAirline(String airline) {
        LambdaQueryWrapper<AirCraft> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AirCraft::getAirline, airline);
        return list(queryWrapper);
    }

    @Override
    public List<AirCraft> listByStatus(String status) {
        LambdaQueryWrapper<AirCraft> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AirCraft::getStatus, status);
        return list(queryWrapper);
    }
    
    @Override
    public boolean save(AirCraft entity) {
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        return super.save(entity);
    }
    
    @Override
    public boolean updateById(AirCraft entity) {
        entity.setUpdateTime(LocalDateTime.now());
        return super.updateById(entity);
    }
}
